/*
 * Copyright (C) 2017, 2019 Samuel Wall
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package uk.co.samuelwall.materialtaptargetprompt.sample.adapter;

import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import uk.co.samuelwall.materialtaptargetprompt.sample.ResourceTable;

import java.util.ArrayList;

/**
 * ListAdapter
 *
 * @since 2021-05-25
 */
public class ListAdapter extends BaseItemProvider {
    private String[] mDataSet;
    private ArrayList<Component> itemViewList = new ArrayList<>();

    /**
     * ListAdapter
     *
     * @param strings strings
     */
    public ListAdapter(String[] strings) {
        if (strings != null) {
            this.mDataSet = strings.clone();
        } else {
            this.mDataSet = null;
        }
    }

    @Override
    public int getCount() {
        return mDataSet.length;
    }

    @Override
    public Object getItem(int i) {
        return mDataSet[i];
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        Component cpt;
        BaseViewHolder viewHolder;
        if (component == null) {
            cpt = LayoutScatter.getInstance(componentContainer.getContext())
                    .parse(ResourceTable.Layout_list_item, null, false);
            viewHolder = new BaseViewHolder(cpt);
            cpt.setTag(viewHolder);
        } else {
            cpt = component;
            viewHolder = (BaseViewHolder) cpt.getTag();
        }
        viewHolder.mText.setText(mDataSet[i]);
        if (mDataSet.length == i + 1) {
            viewHolder.mLine.setVisibility(Component.HIDE);
        } else {
            viewHolder.mLine.setVisibility(Component.VISIBLE);
        }
        if (itemViewList.size() == mDataSet.length) {
            itemViewList.add(i, cpt);
        } else {
            itemViewList.add(cpt);
        }
        return cpt;
    }

    /**
     * getItemView
     *
     * @param count
     * @return itemView
     */
    public Component getItemView(int count) {
        return itemViewList.get(count);
    }

    /**
     * BaseViewHolder
     *
     * @since 2021-05-15
     */
    static class BaseViewHolder {
        Text mText;
        Component mLine;

        BaseViewHolder(Component v) {
            mText = (Text) v.findComponentById(ResourceTable.Id_firstLine);
            mLine = v.findComponentById(ResourceTable.Id_v_line);
        }
    }
}
